﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Components;
using MiniFox.Infrastructure;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace MiniFox.Platform.Jwt
{
    /// <summary>
    /// api后台认证
    /// </summary>
    [AllowAnonymous, NoCache]
    public class JwtController : MvcControllerBase
    {
        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected virtual IJwtService Component
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId"></param>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<string> Issue(
            [FromQuery(Name = OpenApiKey.OPEN_APP_ID), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string appId,
            [FromQuery(Name = OpenApiKey.OPEN_SECRET_KEY), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string secretKey
            )
        {
            var token = this.Component.GetJsonWebToken(appId, secretKey);
            return token.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jwt"></param>
        /// <returns></returns>
        [HttpGet]
        public Response Verify([FromQuery(Name = OpenApiKey.OPEN_ACCESS_TOKEN), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string jwt)
        {
            return this.Component.VerifyJsonWebToken(jwt);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jwt"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<ApplicationIdentity> Identity([FromQuery(Name = OpenApiKey.OPEN_ACCESS_TOKEN), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string jwt)
        {
            JsonWebToken token = jwt;
            var identity = this.Component.GetIdentity(token);
            return identity.CreateResponse();
        }
    }
}
